﻿package com.tdzx.net;

import java.io.IOException;

import javax.net.ssl.SSLHandshakeException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;


import android.content.Context;

public class QHttpClientUtils {

	private static DefaultHttpClient httpClient;
	

	/**
	 * 
	 * @param proxyHost
	 *            代理host
	 * @param proxyPort
	 *            代理port
	 * 
	 *            <pre>
	 *   CMWAP 10.0.0.172:80
	 *   CTWAP 10.0.0.200
	 * </pre>
	 * @return httpClient
	 */
	// public static HttpClient getHttpClient(String proxyHost, int proxyPort) {
	public static HttpClient getHttpClient(Context context) {
		try {

			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
			HttpConnectionParams.setConnectionTimeout(params,20*1000);
//			HttpConnectionParams.setSoTimeout(params, 20*1000);
			HttpConnectionParams.setSocketBufferSize(params, 8192);
			HttpClientParams.setRedirecting(params, true);

			// 设置代理:
			MyHttpCookies li = new MyHttpCookies(context);

			// 可以理解为网络代理商
			String proxyStr = li.getHttpProxyStr();
			if (proxyStr != null && proxyStr.trim().length() > 0) {
				HttpHost proxy = new HttpHost(proxyStr, 80);
				httpClient.getParams().setParameter(
						ConnRouteParams.DEFAULT_PROXY, proxy);
			}

			httpClient = new DefaultHttpClient(params);

			httpClient.setHttpRequestRetryHandler(requestRetryHandler);

//			if (proxyHost != null && proxyHost.trim().length() > 0) {
//				HttpHost proxy = new HttpHost(proxyHost, proxyPort);
//				ConnRouteParams.setDefaultProxy(params, proxy);
//			}

			SchemeRegistry registry = new SchemeRegistry();
			registry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			// registry.register(new Scheme("https", sf, 443));

			// ClientConnectionManager ccm = new
			// ThreadSafeClientConnManager(params, registry);
			// return new DefaultHttpClient(ccm, params);
			return httpClient;
		} catch (Exception e) {
			e.printStackTrace();
			return new DefaultHttpClient();
		}
	}
	
	

	/**
	 * 异常自动恢复处理, 使用HttpRequestRetryHandler接口实现请求的异常恢复
	 */
	private static HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() {
		// 自定义的恢复策略
		public boolean retryRequest(IOException exception, int executionCount,
				HttpContext context) {
			// 设置恢复策略，在发生异常时候将自动重试N次
			if (executionCount >= 3) {
				// 如果超过最大重试次数，那么就不要继续了
				return false;
			}
			if (exception instanceof NoHttpResponseException) {
				// 如果服务器丢掉了连接，那么就重试
				return true;
			}
			if (exception instanceof SSLHandshakeException) {
				// 不要重试SSL握手异常
				return false;
			}
			HttpRequest request = (HttpRequest) context
					.getAttribute(ExecutionContext.HTTP_REQUEST);
			boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
			if (!idempotent) {
				// 如果请求被认为是幂等的，那么就重试
				return true;
			}
			return false;
		}
	};
}
